OpenCV Contrib扩展模块介绍与安装(Python)

您所在的位置:网站首页 pip opencv contrib OpenCV Contrib扩展模块介绍与安装(Python)

OpenCV Contrib扩展模块介绍与安装(Python)

2023-03-29 15:02| 来源: 网络整理| 查看: 265

1.卸载以前的OpenCV 2.安装Contrib版OpenCV 3.Contrib部分模块简介 4.xfeatures2d模块简单测试

OpenCV Contrib是OpenCV的扩展模块,包含了许多最新的以及可能还没有正式发布有待进一步完善的算法,可以理解为是OpenCV的扩展包,Github网页点击查看。 这有点类似于Matlab中的各个可选安装扩展包。 同时在OpenCV 3.0以后,SIFT、SURF等特征算法也放到了Contrib库中。 所以如果想使用SIFT算子,则必须要安装Contrib库, 下面以Python安装OpenCV Contrib库为例,介绍安装流程。 注意Contrib版本的OpenCV是普通OpenCV的超集,包含了所有OpenCV正常版的功能,可以理解为“OpenCV PLUS”。 这里暂时不介绍C++版本Contrib的安装了,因为之前试了挺久但是没有成功,编译时一直出现各种奇怪的问题,百度也百度不到。整个编译过程太痛苦了,弄了一天也没弄好,所以暂时就不说了。而Python版就简单多了,直接PIP即可,也不用编译。 又一次让我深刻感受到“人生苦短,我用Python”的真谛。不管什么方法,完成任务就可以了,不见得非得用编译OpenCV C++源码来折磨自己。

1.卸载以前的OpenCV

如果你以前安装过OpenCV,那么请先卸载干净,否则等Contrib版的装好以后,可能出现无法识别的情况。因为识别的还是之前的版本。 利用PIP命令即可卸载。

pip uninstall opencv-python 2.安装Contrib版OpenCV

Python版的Contrib装起来也非常简单,一行命令即可。

pip install opencv-contrib-python

这里装的是已经预编译好的带有Contrib扩展模块的OpenCV,如果想安装普通版本的OpenCV,直接把opencv-contrib-python换成opencv-python即可。其PIP项目主页在这,简单易用,如果有问题可以去这个主页看看。 安装好以后就可以愉快地使用各种Contrib库了。对于常用的SIFT、SURF等特征提取等模块,放在了cv2.xfeatures2d模块中,其使用放在之后的博客介绍。

[2019-5-5更新]

如果你装Contrib就是为了使用SIFT、SURF等算法,请阅读以下内容。需要注意的是,在3.4.2版本后的opencv-contrib-python中已经移除了non-free模块的相关内容,而SIFT、SURF等则恰恰是在其中的,因此如果直接按照上面方法安装,无法使用SIFT了。 关于这个问题,库的作者在项目主页中也说了,如下。 在Github中的Issue中对这个问题也进行了详细讨论。最终作者的结论是在3.4.2版本以后的库中都不会再加入non-free模块。 因此如果要想使用包含SIFT的non-free模块,只有两种选择,一种是下载3.4.2之前的库(包含)进行安装,例如在pip中输入pip install opencv-contrib-python==3.4.2.17。 第二种是自己下载OpenCV Contrib的源码,开启Non-Free选项,自己用CMake编译。第二种方法自己尝试过,相对来说麻烦一些。所以如果想方便,建议第一种方式。

[更新结束]

3.Contrib部分模块简介

本部分参考Contrib中Github上的相关介绍,摘抄了部分我感兴趣、觉得能用得到的模块。完整版可查看项目网页。

bgsegm: Background segmentation algorithm combining statistical background image estimation and per-pixel Bayesian segmentation. ccalib: Custom Calibration – Patterns for 3D reconstruction, omnidirectional camera calibration, random pattern calibration and multi-camera calibration. cnn_3dobj: Deep Object Recognition and Pose – Uses Caffe Deep Neural Net library to build, train and test a CNN model of visual object recognition and pose. cvv: Computer Vision Debugger – Simple code that you can add to your program that pops up a GUI allowing you to interactively and visually debug computer vision programs. datasets: Datasets Reader – Code for reading existing computer vision databases and samples of using the readers to train, test and run using that dataset’s data. dnn_objdetect: Object Detection using CNNs – Implements compact CNN Model for object detection. Trained using Caffe but uses opencv_dnn modeule. dnns_easily_fooled: Subvert DNNs – This code can use the activations in a network to fool the networks into recognizing something else. face: Face Recognition – Face recognition techniques: Eigen, Fisher and Local Binary Pattern Histograms LBPH methods. optflow: Optical Flow – Algorithms for running and evaluating deepflow, simpleflow, sparsetodenseflow and motion templates (silhouette flow). reg: Image Registration – Pixels based image registration for precise alignment. Follows the paper “Image Alignment and Stitching: A Tutorial”, by Richard Szeliski. rgbd: RGB-Depth Processing module – Linemod 3D object recognition; Fast surface normals and 3D plane finding. 3D visual odometry sfm: Structure from Motion – This module contains algorithms to perform 3d reconstruction from 2d images. The core of the module is a light version of Libmv. structured_light: Structured Light Use – How to generate and project gray code patterns and use them to find dense depth in a scene. surface_matching: Point Pair Features – Implements 3d object detection and localization using multimodal point pair features. tracking: Vision Based Object Tracking – Use and/or evaluate one of 5 different visual object tracking techniques. xfeatures2d: Features2D extra – Extra 2D Features Framework containing experimental and non-free 2D feature detector/descriptor algorithms. SURF, SIFT, BRIEF, Censure, Freak, LUCID, Daisy, Self-similar. ximgproc: Extended Image Processing – Structured Forests / Domain Transform Filter / Guided Filter / Adaptive Manifold Filter / Joint Bilateral Filter / Superpixels / Ridge Detection Filter. xobjdetect: Boosted 2D Object Detection – Uses a Waldboost cascade and local binary patterns computed as integral features for 2D object detection. xphoto: Extra Computational Photography – Additional photo processing algorithms: Color balance / Denoising / Inpainting.

更多关于这些模块的使用教程,可以在Contrib的Github项目中找到,每个模块基本都有对应的samples,方便学习。 这篇博客把OpenCV各个模块也简单介绍了下,可以看看。 还有个不错的OpenCV教程网站,国外的,有空可以看看,点击查看。

4.xfeatures2d模块简单测试

下面是使用cv2.xfeatures2d模块中SIFT和SURF算子的简单示例代码。

# coding=utf-8 import cv2 import numpy as np # 读取图像 img = cv2.imread("camera.png") # 创建对象 # 对于SIFT算子,可以通过nFeatures属性控制特征点数量 # 对于SURF算子,可以通过hessianThreshold属性控制特征点数量 # 更详细的用法见OpenCV官方API文档 SIFT = cv2.xfeatures2d_SIFT.create() SURF = cv2.xfeatures2d_SURF.create() # 提取特征并计算描述子 kps, des = cv2.xfeatures2d_SIFT.detectAndCompute(SIFT, img, None) kps2, des2 = cv2.xfeatures2d_SURF.detectAndCompute(SURF, img, None) # 新建一个空图像用于绘制特征点 img_sift = np.zeros(img.shape, np.uint8) img_surf = np.zeros(img.shape, np.uint8) # 绘制特征点 cv2.drawKeypoints(img, kps, img_sift) cv2.drawKeypoints(img, kps2, img_surf) # 展示 cv2.imshow("img", img) cv2.imshow("sift", img_sift) cv2.imshow("surf", img_surf) cv2.waitKey(0)

可以发现使用不同的算子非常简单,在代码中甚至只需要修改创建和检测部分的函数名称就可以了,其余参数等全部都不用改变,基本流程都是一样的。 下面是使用默认参数时SIFT、SURF的特征点提取结果。 原始图像 SIFT特征提取结果 SURF特征提取结果



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3